<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<addForm v-show="addStatus" ref="addFormRef" />

		<view v-show="!addStatus">
			<u-form :model="dataForm" ref="dataForm" :errorType="['toast']" label-position="left" label-width="150"
				label-align="left">
				<u-form-item label="借款单号" v-if="judgeShow('ti002')">
					<u-input v-model="dataForm.ti002" disabled></u-input>
				</u-form-item>
				<u-form-item label="借款金额" v-if="judgeShow('ti004')">
					<u-input v-model="dataForm.ti004" :disabled="judgeWrite('ti004')|| setting.readonly"></u-input>
				</u-form-item>
				<u-form-item label="借款日期" v-if="judgeShow('ti005')">
					<u-input v-model="dataForm.ti005" :disabled="judgeWrite('ti004')|| setting.readonly"></u-input>
				</u-form-item>
				<u-form-item label="归还日期" v-if="judgeShow('ti006')">
					<u-input v-model="dataForm.ti006" :disabled="judgeWrite('ti004')|| setting.readonly"></u-input>
				</u-form-item>
				<u-form-item label="还款金额" v-if="judgeShow('ti010')">
					<u-input v-model="dataForm.ti010" disabled></u-input>
				</u-form-item>
				<u-form-item label="收款人姓名" v-if="judgeShow('ti008')">
					<jnpf-user-select v-model="dataForm.ti008" @input="changeName"
						:disabled="judgeWrite('ti008')|| setting.readonly" />
				</u-form-item>
				<u-form-item label="部门" v-if="judgeShow('ti014')">
					<u-input v-model="dataForm.ti014" disabled></u-input>
				</u-form-item>
				<u-form-item label="借款前余额" v-if="judgeShow('ti015')">
					<u-input v-model="dataForm.ti015" disabled></u-input>
				</u-form-item>
				<u-form-item label="借款额度" v-if="judgeShow('ti016')">
					<u-input v-model="dataForm.ti016" disabled></u-input>
				</u-form-item>
				<u-form-item label="剩余额度" v-if="judgeShow('ti017')">
					<u-input v-model="dataForm.ti017" disabled></u-input>
				</u-form-item>
				<u-form-item label="借款事由" v-if="judgeShow('ti003')">
					<u-input v-model="dataForm.ti003" :disabled="judgeWrite('ti003')|| setting.readonly"></u-input>
				</u-form-item>
				<u-form-item label="备注" v-if="judgeShow('ti011')">
					<u-input v-model="dataForm.ti011" :disabled="judgeWrite('ti011')|| setting.readonly"></u-input>
				</u-form-item>
				<u-form-item label="附件" v-if="judgeShow('ti012')">
					<jnpf-file v-model="dataForm.ti012" :disabled="judgeWrite('ti012')|| setting.readonly" />
				</u-form-item>
			</u-form>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import addForm from "./addForm.vue";
	import comMixin from "../mixin";
	import {
		getGoodsList,
		getCustomerList,
		Info,
		Create,
		Update,
	} from "@/api/apply/order";
	import {
		getOasti,
		BomcbListInit,
		putOasti
	} from "@/api/workFlow/flowBefore";
	import {
		getDictionaryDataSelector,
		getDataInterfaceDataInfo,
		getUserAll
	} from "@/api/common";
	export default {
		mixins: [comMixin],
		components: {
			addForm
		},
		computed: {
			getTb019Computed() {
				return (id) => {
					const options = this.options.filter((item) => id.includes(item.id));
					return options.length > 0 ? options[0].fullName : "";
				};
			},
		},
		data() {
			return {
				opType: 0,
				show: false,
				dataForm: {},
				addStatus: "",
				id: "",
				showForm: [],
				formOperatesList: [],
				tb019Options: [],
				tabsList: [{
						name: "真实情况",
					},
					{
						name: "发票情况",
					},
				],
				current: 0,
			};
		},
		methods: {
			//根据用户获取员工姓名的借款额度，借款余额
			getBorrowMoneyInfo() {
				var queryParam = {
					ti008: this.dataForm.ti008,
				};
				request({
					url: "/api/SubDev/Oasti/getBorrowMoneyInfo",
					method: "get",
					data: queryParam,
				}).then((res) => {
					this.dataForm.ti015 = res.data.beforeLoanBalance;
					this.dataForm.ti016 = res.data.mv080;
					this.dataForm.ti017 = res.data.mv081;
				});
			},
			changeName(id) {
				getUserAll().then(res => {
					let item = res.data.filter(o => o.id === id)[0]
					this.dataForm.ti013 = item.departmentId.departmentId;
					this.dataForm.ti014 = item.department;
					this.getBorrowMoneyInfo()
				})
			},
			init(data, id, flowFormContentJson, addStatus) {
				this.addStatus = addStatus
				this.opType = data.opType;
				this.setting = data;
				this.formOperatesList = data.formOperates;
				this.id = id;
				if (!this.addStatus) {
					this.getInfoFn(id);
				} else {
					console.log('新增');
					this.$refs.addFormRef.getInfoFn(data)

				}
			},
			getInfoFn(id) {
				getOasti(id).then((res) => {
					if (res.code == 200) {
						this.dataForm = res.data;
						this.getDataInterfaceDataInfoME001(this.dataForm.tg002);
						this.getDataInterfaceDataInfoMV001(this.dataForm.tg011);
					}
				});
			},
			judgeShow(id) {
				if (!this.setting.formOperates || !this.setting.formOperates.length)
					return true;
				let arr = this.setting.formOperates.filter(o => o.id === id) || [];
				if (!arr.length) return true;
				let item = arr[0];
				return item.read;
			},
			judgeWrite(id) {
				if (!this.setting.formOperates || !this.setting.formOperates.length)
					return false;
				let arr = this.setting.formOperates.filter(o => o.id === id) || [];
				if (!arr.length) return true;
				let item = arr[0];
				return !item.write;
			},
			change(e) {
				this.current = e;
			},
			queryChildBachelor(tb004) {
				let query = {
					tb004,
				};
				BomcbListInit(query).then((res) => {
					this.showForm = res.data;
					this.show = true;
				});
			},
			//  申请人
			getDataInterfaceDataInfoME001(id) {
				let data = {
					id,
					interfaceId: "445425382452126213",
					propsValue: "MV001",
					relationField: "JoinStr",
				};
				getDataInterfaceDataInfo("445425382452126213", data).then((res) => {
					this.dataForm.tg002 = res.data.JoinStr;
				});
			},
			// 部门
			getDataInterfaceDataInfoMV001(id) {
				let data = {
					id,
					interfaceId: "362111557984852549",
					propsValue: "ME001",
					relationField: "JoinStr",
				};
				getDataInterfaceDataInfo("362111557984852549", data).then((res) => {
					this.dataForm.tg011 = res.data.JoinStr;
				});
			},
			selfGetInfo() {
				Info(this.setting.id).then((res) => {
					this.dataForm = res.data;
					if (this.dataForm.fileJson) {
						this.fileList = JSON.parse(this.dataForm.fileJson);
					}
				});
			},
			submit(eventType, config) {
				console.log('config===>', config);
				this.dataForm.flowId = config.flowId
				this.dataForm.flowState = eventType === "submit" ? 0 : 1;
				//限制借款金额不能超过剩余额度
				if (this.dataForm.ti004 > this.dataForm.ti017) {
					this.dataForm.ti004 = 0;
					return this.$refs.uToast.show({
						title: "借款金额不能超过剩余额度",
						type: "error",
					});
				}
				if (this.jnpf.isEmpty(this.dataForm.ti004) || this.dataForm.ti004 == 0) {
					return this.$refs.uToast.show({
						title: "借款金额不能为空/0",
						type: "error",
					});
				}
				if (eventType === "audit" || eventType === "reject") {
					this.$emit("eventReceiver", this.dataForm, eventType);
					return;
				}
				let name = eventType === "submit" ? '提交' : '保存';
				uni.showModal({
					title: "提示",
					content: `您确定要${name}当前流程吗？`,
					success: (res) => {
						if (res.confirm) {
							this.selfHandleRequest();
						}
					},
				});
			},
			selfHandleRequest() {
				putOasti(this.dataForm.ti001, this.dataForm).then((res) => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit("refresh");
								uni.navigateBack();
							}, 1500);
						},
					});
				});
			},
			initData() {
				getGoodsList().then((res) => {
					this.goodsOptions = res.data.list;
				});
				getCustomerList().then((res) => {
					this.customerOptions = res.data.list;
				});
			},
			selfInit() {
				this.addGoods();
				this.addPlan();
			},
			onChange(e) {
				const user = e[0];
				this.dataForm.salesmanName = user.fullName;
			},
			addGoods() {
				const item = {
					goodsId: "",
					goodsCode: "",
					goodsName: "",
					specifications: "",
					unit: "",
					qty: "",
					price: "",
					amount: "",
					discount: null,
					cess: null,
					actualPrice: "",
					actualAmount: "",
					description: "",
				};
				this.dataForm.goodsList.push(item);
			},
			delGoods(index) {
				this.dataForm.goodsList.splice(index, 1);
			},
			addPlan() {
				const item = {
					receivableDate: "",
					receivableRate: "",
					receivableMoney: "",
					receivableMode: "",
					abstract: "",
				};
				this.dataForm.collectionPlanList.push(item);
			},
			delPlan(index) {
				this.dataForm.collectionPlanList.splice(index, 1);
			},
			onGoodsChange(e, i) {
				const goods = e[0];
				this.dataForm.goodsList[i] = {
					goodsId: goods.id,
					goodsCode: goods.code,
					goodsName: goods.text,
					specifications: goods.specifications,
					unit: goods.unit,
					qty: 1,
					price: goods.price,
					amount: goods.price,
					discount: 100,
					cess: 0,
					actualPrice: goods.price,
					actualAmount: goods.price,
					description: "",
				};
				this.$forceUpdate();
			},
			onCustomerChange(e) {
				const customer = e[0];
				this.dataForm.customerName = customer.text;
			},
			count(row) {
				//金额 = 数量*单价
				row.amount = this.jnpf.toDecimal(
					parseFloat(row.price) * parseFloat(row.qty)
				);
				//折扣价 = (单价*折扣)
				var discountPrice = row.price * (row.discount / 100);
				//实际单价 = 折扣价 * (1 + (税率 / 100))
				row.actualPrice = this.jnpf.toDecimal(
					discountPrice * (1 + row.cess / 100)
				);
				//实际金额
				row.actualAmount = this.jnpf.toDecimal(
					parseFloat(row.actualPrice) * parseFloat(row.qty)
				);
				this.$forceUpdate();
			},
		},
	};
</script>